local joy__xushen = fk.CreateSkill {

  name = "joy__xushen",

  tags = { Skill.Limited, },

}



joy__xushen:addEffect(fk.EnterDying, {
  name = "joy__xushen",
  anim_type = "defensive",
  frequency = Skill.Limited,
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(joy__xushen.name) and player.dying and player:usedSkillTimes(joy__xushen.name, Player.HistoryGame) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:recover({
      who = player,
      num = 1,
      recoverBy = player,
      skillName = joy__xushen.name
    })
    room:handleAddLoseSkills(player, "ty__zhennan", nil, true, false)
    if player.dead or table.find(room.alive_players, function(p) return string.find(p.general, "guansuo") end) then return end
    local targets = table.map(room:getOtherPlayers(player), Util.IdMapper)
    local to = room:askForChoosePlayers(player, targets, 1, 1, "#joy__xushen-choose", joy__xushen.name, true)
    if #to > 0 then
      to = room:getPlayerById(to[1])
      if room:askForSkillInvoke(to, joy__xushen.name, nil, "#joy__xushen-invoke") then
        if player.general == "joy__baosanniang" then 
          room:notifySkillInvoked(player, "joy__xushen","drawcard")
          room:changeHero(player,"joy_skin__baosanniang",false,false,true,false)
          room:notifySkillInvoked(to, "joy__xushen","drawcard")
            room:changeHero(to, "joy_skin__guansuo", false, false, true)
            if not to.dead then
        to:drawCards(3, joy__xushen.name)
      end
        elseif player.deputyGeneral == "joy__baosanniang" then
          room:notifySkillInvoked(player, "joy__xushen","drawcard")
          room:changeHero(player,"joy_skin__baosanniang",false,true,true,false)
          room:notifySkillInvoked(to, "joy__xushen","drawcard")
        room:changeHero(to, "joy_skin__guansuo", false, false, true)
        if not to.dead then
        to:drawCards(3, joy__xushen.name)
      end
        else
        room:changeHero(to, "joy__guansuo", false, false, true)
        if not to.dead then
        to:drawCards(3, joy__xushen.name)
      end
        end
      end
    end
  end,
})

return joy__xushen